﻿using DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BUS;

namespace QLVBDQ
{
    /// <summary>
    /// Interaction logic for TimKiemSanPham.xaml
    /// </summary>
    public partial class TimKiemSanPham : UserControl
    {
        public TimKiemSanPham()
        {
            InitializeComponent();
            
        }

        public void LoadTypeProduce()
        {
            List<LoaiSanPham_DTO> list = new List<LoaiSanPham_DTO>();
            list = LoaiSanPham_BUS.Instance.LayToanBoLoaiSP();
            if (list == null)
            {
                return;
            }
            for (int i = 0; i < list.Count;i++ )
            {
                this.loaiSP.Items.Add(list[i].TenLoaiSanPham);
            }
        }


        private void btnTim_Click(object sender, RoutedEventArgs e)
        {
            if (loaiSP.SelectedItem == null)
            {
                MessageBox.Show("Bạn phải chọn 1 loại sản phẩm để tìm kiếm");
                return;
            }
            SanPham_DTO item = new SanPham_DTO(this.maSP.Text.ToString(), this.tenSP.Text.ToString(), "", this.loaiSP.SelectedItem.ToString(), 0, 0);
            ShowResult(item);
        }

        public void ShowResult(SanPham_DTO item)
        {
            List<SanPham_DTO> list = SanPham_BUS.Instance.TimKiemSanPham(item);
            if (list==null)
            {
                MessageBox.Show("Không Thể Tìm Thấy Kết Quả!");
                this.listSanPham.Items.Clear();
                return;
            }
            this.listSanPham.Items.Clear();
            for (int i = 0; i < list.Count;i++ )
            {
                list[i].STT = i + 1;
                //list[i].TenLoaiSanPham = list[i].LoaiSP.TenLoaiSanPham;
                this.listSanPham.Items.Add(list[i]);
            }
            this.listSanPham.Items.Refresh();
        }

        private void UserControl_Loaded(object sender, EventArgs e)
        {
            LoadTypeProduce();
        }
    }
}
